package org.example.forum.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.dao.MessageMapper;
import org.example.forum.exception.ApplicationException;
import org.example.forum.model.Message;
import org.example.forum.model.User;
import org.example.forum.service.IMessageService;
import org.example.forum.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.crypto.spec.RC2ParameterSpec;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class messageServiceImpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;

    @Resource
    private IUserService userService;

    @Override
    public void create(Message message) {
        if(message==null || message.getPostUserId()==null || message.getReceiveUserId()==null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验接受者是否存在
        User user=userService.selectById(message.getReceiveUserId());
        if(user==null || user.getDeleteState()==1){
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //设置默认值
        message.setState((byte)0);
        message.setDeleteState((byte)0);
        Date date =new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);

        int row = messageMapper.insertSelective(message);
        if(row!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.ERROR_SERVICES));
        }
        log.info("发送完成");
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        //非空校验
        if(receiveUserId==null || receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return messageMapper.selectUnreadCount(receiveUserId);
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if(receiveUserId==null || receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return messageMapper.selectByReceiveUserId(receiveUserId);
    }

    @Override
    public void updateStateById(Long id,Byte state) {
        // 0未读 1已读 2已回复
        if(id==null || id<=0 || state==null || state<=0 || state>2){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message updateMessage=new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        updateMessage.setUpdateTime(new Date());
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if(row!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.ERROR_SERVICES));
        }
        log.info("私信状态修改成功");
    }

    @Override
    public Message selectById(Long id) {
        if(id==null || id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {
        //非空校验
        if(repliedId==null || repliedId<=0 ){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //检验repliedId对应的私信
        Message existMessage = messageMapper.selectByPrimaryKey(repliedId);
        if(existMessage==null || existMessage.getDeleteState()==1){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        updateStateById(repliedId, (byte)2);
        create(message);
    }


}
